#include <cstdio>
#include <cstring>
#include <bitset>

using namespace std;

namespace my {
    const int maxplen=100, maxslen=1000000;
    char p[maxplen+2], s[maxslen+2];
    int next[maxplen+2], plen=0, slen=0;
    bitset<maxplen+1> f[maxslen+2];

    void buildNext() {
        static int sta[maxplen+1];
        for (int i=1; i<=plen; i++) {
            if (p[i]=='[') sta[++sta[0]] = i;
            else if (p[i]==']') next[sta[sta[0]--]] = i;
        }
    }

    void go(int x, int y) {
        if (y<plen) {
            if (p[y]=='*') f[x+1][y] = true;
            if (p[y+1]=='*') {
                f[x+1][y+1] = true;
                if (p[y+2]==s[x+1]) f[x+1][y+2] = true;
            }
            if (p[y+1]=='[') {
                f[x][y+1] = true;
                f[x][next[y+1]] = true;
                if (p[y+2]==s[x+1]) f[x+1][y+2] = true;
                if (p[next[y+1]+1]==s[x+1]) f[x+1][next[y+1]+1] = true;
            }
            if (p[y+1]==']') {
                f[x][y+1] = true;
                if (p[y+2]==s[x+1]) f[x+1][y+2] = true;
            }
            if (p[y+1]==s[x+1] || p[y+1]=='?') f[x+1][y+1] = true;
        }
    }

    int main() {
        freopen("walking.in", "r", stdin);
        freopen("walking.out", "w", stdout);

        int n;
        scanf("%s %d", p+1, &n);
        plen = strlen(p+1);
        buildNext();
        for (int i=1; i<=n; i++) {
            scanf("%s", s+1);
            slen = strlen(s+1);
            f[0][0] = 1;
            for (int j=0; j<=slen; j++) {
                f[j+1] = 0;
                for (int k=0; k<=plen; k++) {
                    if (f[j][k]) {
                        go(j, k);
                    }
                }
                if (j) printf("%d", (int)f[j].test(plen));
            }
            printf("\n");
        }

        fclose(stdin);
        fclose(stdout);
        return 0;
    }
};

int main() {return my::main();}
